package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.CarCheckInfoBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.JsonUtil;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/11 0011
 */
public class CarCheckRecordActivity extends BaseSecondActivity {
    @BindView(R.id.todaycarcheck_head)
    ExpandHeadView mTodaycarcheckHead;
    @BindView(R.id.recordListview)
    MyListView mRecordListview;
    private ArrayList<CarCheckInfoBean> mdatas = new ArrayList<>();

    @Override
    protected int setContentView() {
        return R.layout.activity_carcheckrecord;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mTodaycarcheckHead;
    }

    @Override
    protected void initView() {
        initData();
    }

    private void initData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("sign", Constants.SIGN);
        params.put("userId", SPUtils.getString(Config.userId, ""));
        params.put("vehicleId", SPUtils.getString("vehicleId", ""));
        params.put("searchDate", "");

        OkHttpUtils.post().url(Constants.FINDCHECKLISTBYVEHICLE_URL)
                .params(params).build().execute(new StringCallback() {
                                                    @Override
                     public void onError(Call call, Exception e, int id) {
                         MyToast.show("网络开了个小差");
                     }
                     @Override
                     public void onResponse(String response, int id) {
                         ALog.d(response);
                         try {
                             JSONObject jsonObject = new JSONObject(response);
                             int success = Integer.parseInt((String) jsonObject.get("success"));
                             String message = jsonObject.getString("message");
                             if (success == 0) {
                                 String map = jsonObject.getString("map");
                                 HashMap<String, Object> hashMap = JsonUtil.parseJsonToMap(map);
                                 for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
                                     ArrayList<String> values = (ArrayList<String>) entry.getValue();
                                     int length = values.size();
                                     CarCheckInfoBean carCheckInfoBean = null;
                                     for (int i = 0; i < length; i++) {
                                         carCheckInfoBean = new CarCheckInfoBean();
                                         carCheckInfoBean.setDate(entry.getKey());
                                         carCheckInfoBean.setCode(values.get(0));
                                         if (length > 1) {
                                             if (values.get(1).contains("group")) {
                                                 String split = values.get(1);
                                                 // .substring(0,values.get(1).length()-1)
                                                 carCheckInfoBean.setPhotoPaths(split.split(","));
                                             } else {
                                                 carCheckInfoBean.setRemarks(values.get(1));
                                             }
                                         }
                                     }
                                     mdatas.add(carCheckInfoBean);

                                 }
                                 Collections.sort(mdatas, new Comparator<CarCheckInfoBean>() {
                                     @Override
                                     public int compare(CarCheckInfoBean o1, CarCheckInfoBean o2) {
                                         return (o2.getDate()).compareTo(o1.getDate());
                                     }
                                 });
                                 mRecordListview.setAdapter(new CarCheckRecordAdapter(mdatas));

                             } else {
                                 MyToast.show(message);
                             }
                         } catch (JSONException e) {
                             e.printStackTrace();
                         }
                     }
                 }

        );
    }

    private class CarCheckRecordAdapter extends MyBaseAdapter<CarCheckInfoBean> {


        private CarCheckRecordAdapter(List<CarCheckInfoBean> dataSets) {
            super(dataSets);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(UIUtils.getContext(), R.layout.item_carcheckrecord, null);
                viewHolder = new ViewHolder();
                viewHolder.mTvDate = (TextView) convertView.findViewById(R.id.tvDate);
                viewHolder.mRemarks = (TextView) convertView.findViewById(R.id.remarks);
                viewHolder.mIscheck = (ImageView) convertView.findViewById(R.id.ischeck);
                viewHolder.mReviewphoto = (ImageView) convertView.findViewById(R.id.reviewphoto);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final CarCheckInfoBean carCheckInfoBean = mDataSets.get(position);
            viewHolder.mTvDate.setText(carCheckInfoBean.getDate());
            int code = Integer.parseInt(carCheckInfoBean.getCode());
            //1.已核查 数组第二个值为图片路径 2.已备注 数组第二个值为备注信息 5.未核查
            if (code == 5) {
                viewHolder.mIscheck.setImageResource(R.drawable.cuowu_p_2x);      //未核查
                viewHolder.mReviewphoto.setVisibility(View.GONE);
                viewHolder.mRemarks.setVisibility(View.GONE);
            } else if (code == 2) {
                viewHolder.mIscheck.setImageResource(R.drawable.about_1a_2x);       //已备注
                viewHolder.mRemarks.setVisibility(View.VISIBLE);
                viewHolder.mReviewphoto.setVisibility(View.GONE);
                viewHolder.mRemarks.setText(carCheckInfoBean.getRemarks());
            } else if (code == 1) {
                viewHolder.mIscheck.setImageResource(R.drawable.right_p_2x);       //有图片

                viewHolder.mRemarks.setVisibility(View.GONE);
                viewHolder.mReviewphoto.setVisibility(View.VISIBLE);
                viewHolder.mReviewphoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String[] photoPaths = carCheckInfoBean.getPhotoPaths();
                        Intent intent = new Intent(UIUtils.getContext(), SwitchImageActivity.class);
                        intent.putExtra("imagepath", photoPaths);
                        startActivity(intent);
                    }
                });
            }


            return convertView;
        }

        private class ViewHolder {
            TextView mTvDate;
            TextView mRemarks;
            ImageView mIscheck;
            ImageView mReviewphoto;
        }
    }


}
